$(function () {
    $("#jqGrid").Grid({
        url: '../apkversion/list',
        colModel: [
            {label: '版本名称', name: 'name', index: 'NAME', width: 80},
            {
                label: '客户端类别',
                name: 'clientType',
                index: 'CLIENT_TYPE',
                width: 40,
                formatter: function (value, col, row) {
                    return sysEnums.APP_CLIENT_TYPE[row.clientType];
                }
            },
            {label: '应用版本名称', name: 'version', index: 'VERSION', width: 60},
            {label: '应用版本号', name: 'vno', index: 'V_NO', width: 60},
            {label: 'APK名称', name: 'apkName', index: 'APK_NAME', width: 90, sortable: false},
            {label: '下载地址', name: 'url', index: 'URL', sortable: false},
            {label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80},
            {label: '备注信息', name: 'remark', index: 'REMARK', width: 80},
            {
                label: '操作', width: 40, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.eyeFile(\"" + row.url + "\")'><i class='fa fa-eye'></i>&nbsp;下载</button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        apkVersion: {version: '', apkName: '', url: '', name: '', id: '', remark: '', clientType: ''},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ],
            version: [
                {required: true, message: '应用版本名称必填', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        picUrl: "",
        appClientTypeList: enumsToListWithoutChange('APP_CLIENT_TYPE')
    },
    methods: {
        query: function () {
            vm.reload();
        },

        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.apkVersion = {
                version: '',
                apkName: '',
                url: '',
                name: '',
                id: '',
                remark: '',
                clientType: 'PAD'
            };
            vm.picUrl = '';
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.apkVersion.id == (null || '') ? "../apkversion/save" : "../apkversion/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.apkVersion),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../apkversion/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../apkversion/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.apkVersion = r.apkVersion;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles()
            this.$Loading.start();
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccess: function (res, file) {
            vm.apkVersion.url = file.response.url;
            var url = vm.apkVersion.url;
            var relName = file.response.name;
            vm.apkVersion.apkName = relName;
            if (url) {
                this.$Loading.finish();
                iview.Message.success('文件上传成功');
                vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length);
            }
        },
        eyeFile: function (url) {
            if (url) {
                window.open(url);
            } else {
                alert('还未上传文件');
            }
        },
    }
});
